//
//  AView.m
//  Study1
//
//  Created by ChenJie on 2/7/20.
//  Copyright © 2020 OrgName. All rights reserved.
//

#import "AView.h"

@interface AView ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton;

@end

@implementation AView

#pragma mark - Lifecycle methods

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor lightGrayColor];
        
        [self addSubview:self.imageView];
        self.imageView.image = [UIImage imageNamed:@"1"];
        
        //
//        if ([self.delegate respondsToSelector:@selector(iconForAview:)]) {
//            self.imageView.image = [self.delegate iconForAview:self];
//        }
        
        [self addSubview:self.leftButton];
        [self addSubview:self.rightButton];
        
        self.clipsToBounds = YES;
    }
    return self;
}

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];
}

- (void)didMoveToSuperview {
    [super didMoveToSuperview];
}

- (void)willMoveToWindow:(UIWindow *)newWindow {
    [super willMoveToWindow:newWindow];
}

- (void)didMoveToWindow {
    [super didMoveToWindow];
}



- (void)layoutSubviews {
    [super layoutSubviews];
    
    //
    CGFloat imageWidth = 50;
    CGFloat imageHeight = 50;
    CGFloat buttonHeight = 30;
    
    //
    self.imageView.frame =
    CGRectMake(0, (self.frame.size.height - buttonHeight - imageHeight)/2, imageWidth,  imageHeight);
    
    
    //
    CGFloat buttonTop = self.frame.size.height - buttonHeight;
    CGFloat buttonWidth = self.frame.size.width / 2.0f;
    self.leftButton.frame = CGRectMake(0, buttonTop, buttonWidth, buttonHeight);
    self.rightButton.frame = CGRectMake(buttonWidth+5, buttonTop, buttonWidth-5, buttonHeight);
    
    
}

#pragma mark - Public methods

- (void)goLeft {
    CGRect frame = self.imageView.frame;
    frame.origin.x -= 10;
    self.imageView.frame = frame;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(AView:frameDidChange:)]) {
        [self.delegate AView:self frameDidChange:frame];
    }
}

- (void)goRight {
    CGRect frame = self.imageView.frame;
    frame.origin.x += 10;
    self.imageView.frame = frame;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(AView:frameDidChange:)]) {
        [self.delegate AView:self frameDidChange:frame];
    }
}

#pragma mark - Private methods



#pragma mark - Event response methods

- (void)leftButtonClick:(UIButton *)button {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(AViewLeftButtonWillClick:)]) {
        [self.delegate AViewLeftButtonWillClick:self];
    }
    
    [self goLeft];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(AViewLeftButtonDidClick:)]) {
        [self.delegate AViewLeftButtonDidClick:self];
    }
}

- (void)rightButtonClick:(UIButton *)button {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(AViewRightButtonDidClick:)]) {
        [self.delegate AViewRightButtonDidClick:self];
    }
    
    
    [self goRight];
}

#pragma mark - Delegate methods



#pragma mark - Setter

- (void)setDelegate:(id<AViewDelegate>)delegate {
    _delegate = delegate;
    
    //
    if ([delegate respondsToSelector:@selector(iconForAview:)]) {
        self.imageView.image = [delegate iconForAview:self];
    }
}

#pragma mark - Getter

- (UIImageView *)imageView {
    if (!_imageView) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        imageView.backgroundColor = [UIColor redColor];
        
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.clipsToBounds = YES;
        
        _imageView = imageView;
    }
    return _imageView;
}

- (UIButton *)leftButton {
    if (!_leftButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        //
        button.backgroundColor = [UIColor grayColor];
        [button setTitle:@"Left" forState:UIControlStateNormal];
        //
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        // target-action
        [button addTarget:self
                   action:@selector(leftButtonClick:)
         forControlEvents:UIControlEventTouchUpInside];
        
        _leftButton  = button;
    }
    return _leftButton;
}

- (UIButton *)rightButton {
    if (!_rightButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        //
        button.backgroundColor = [UIColor grayColor];
        [button setTitle:@"Right" forState:UIControlStateNormal];
        //
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        // target-action
        [button addTarget:self
                   action:@selector(rightButtonClick:)
         forControlEvents:UIControlEventTouchUpInside];
        
        _rightButton  = button;
    }
    return _rightButton;
}

#pragma mark - Memory management methods

- (void)dealloc {
}

@end
